<html>
<body>
<?php
require_once('adodb/adodb.inc.php'); 

$conn =& ADONewConnection('mysql');	
$conn->Connect('localhost','root','','prepprofiles');
$conn->SetFetchMode(ADODB_FETCH_ASSOC); 


if ($_GET['table']!="") {
	$ar=$conn->MetaColumns($_GET['table']);	
	$form="";
	echo("<h1>Definition for table ".$_GET['table'].":</h1><BR>");
	
	echo('<xmp>$DBT=new MSDBTable("'.$_GET['table'].'");
');
	foreach ($ar as $f) {

		$maxlen="";
		$isBit=false;
		
		switch ($f->type) {
			case "char":
			case "nchar":
			case "text":
			case "ntext":
			case "smalltext":
			case "varchar":
			case "nvarchar":
				echo('$DBT->addVarcharField("' . $f->name . '","' . $f->name . '",-1,' . $f->max_length . ');
');
				$maxlen=' maxlength="'.$f->max_length.'"';
				break;
				
			case "int":
			case "tinyint":
			case "numeric":
			case "money":
			case "decimal":
			case "float":
				echo('$DBT->addIntField("' . $f->name . '","' . $f->name . '",-1,-1);
');
				$maxlen=' maxlength="15"';
				break;
				
			case "bit":
				echo('$DBT->addBitField("' . $f->name . '","' . $f->name . '");
');
				$isBit=true;
				break;
				
			case "datetime":
				echo('$DBT->addDateField("' . $f->name . '","' . $f->name . '");
');
				$maxlen=' maxlength="10"';
				break;
				
			default:
				echo("[TYPE NOT FOUND]: " . $f->type . " - Field: " . $f->name) . "
";
				break;
		}

		if ($isBit)
			$form .= $f->name . ' <input type="checkbox" name="' . $f->name . '" value="1">
';
		else
			$form .= $f->name . ' <input type="text" name="' . $f->name . '" value=""'.$maxlen.'>
';
	}
	
	$pk=$conn->MetaPrimaryKeys($_GET['table']);
	foreach ($pk as $f) {
		echo('$DBT->setPK("' . $f . '");<BR>');
	}

	echo('
	
	
$DBC=new MSDBCommon($_POST,$DBT);
');
	echo('$DBC->validateFieldsForInsert();
');
	echo('$DBC->getInsertString();
');
	echo('$DBC->validateFieldsForUpdate();
');
	echo('$DBC->getUpdateString();


');
	echo($form.'</xmp>');
} 


echo("<BR><BR><h1>Available tables:</h1><BR>");
foreach ($conn->MetaTables('TABLES') as $table) {
	echo("<a href='?table=$table'>$table</a><br>");	
}

$conn->Close();
?>
</body>
</html>
